package com.siderakis.gwt.cellwidgets.client.canvas;

import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;

public class Canvas extends Composite implements HasAllMouseHandlers {
	private final HTML html;
	private final Element canvas;

	public Canvas() {
		html = new HTML("<canvas></canvas>"); //$NON-NLS-1$
		initWidget(html);
		canvas = (Element) html.getElement().getFirstChild();
	}

	@Override
	public HandlerRegistration addMouseDownHandler(final MouseDownHandler handler) {
		return html.addMouseDownHandler(handler);
	}

	@Override
	public HandlerRegistration addMouseMoveHandler(final MouseMoveHandler handler) {
		return html.addMouseMoveHandler(handler);
	}

	@Override
	public HandlerRegistration addMouseOutHandler(final MouseOutHandler handler) {
		return html.addMouseOutHandler(handler);
	}

	@Override
	public HandlerRegistration addMouseOverHandler(final MouseOverHandler handler) {
		return html.addMouseOverHandler(handler);
	}

	@Override
	public HandlerRegistration addMouseUpHandler(final MouseUpHandler handler) {
		return html.addMouseUpHandler(handler);
	}

	@Override
	public HandlerRegistration addMouseWheelHandler(final MouseWheelHandler handler) {
		return html.addMouseWheelHandler(handler);
	}

	public native RenderingContext getContext() /*-{
		return this.@com.siderakis.gwt.cellwidgets.client.canvas.Canvas::canvas.getContext("2d");
	}-*/;

	public void setCanvasSize(final int width, final int height) {
		DOM.setElementPropertyInt(canvas, "width", width); //$NON-NLS-1$
		DOM.setElementPropertyInt(canvas, "height", height); //$NON-NLS-1$
	}
}
